﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HotDealD2.Models.ModelView;

namespace HotDealD2.Models.ModelManager
{
    public class OrderDealModel
    {
        private QLDataBaseDataContext dataContext;
        public OrderDealModel()
        {
            dataContext = new QLDataBaseDataContext();
        }
        public List<DonHang> GetAllDonHang()
        {
            var listDonHang = from list in dataContext.DonHangs select list;
            return listDonHang.ToList();
        }

        public bool DeleteOrder(int id)
        {
            try
            {

                DonHang donhang = (from d in dataContext.DonHangs
                                   where d.MaDonHang == id
                                   select d).First();

               

                List<DanhSachSanPham> listsanpham = (from order in dataContext.DanhSachSanPhams
                                                   where order.MaDonHang == donhang.MaDonHang
                                                   select order).ToList();
                for (int i = 0; i < listsanpham.Count; i++)
                {
                    DanhSachSanPham sanpham = (from d in dataContext.DanhSachSanPhams
                                       where d.MaDonHang == listsanpham[i].MaDonHang 
                                       select d).First();
                    dataContext.DanhSachSanPhams.DeleteOnSubmit(sanpham);
                    dataContext.SubmitChanges();
                }
                dataContext.DonHangs.DeleteOnSubmit(donhang);
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateOrder(OrderDealView orderdealview)
        {
            try
            {
                DonHang dh = dataContext.DonHangs.Single(ltv => ltv.MaDonHang == orderdealview.MaDonHang);
                if (dh == null)
                {
                    return false;
                }
                dh.TenNguoiNhan = orderdealview.TenNguoiNhan;
                dh.SoDienThoaiNguoiNhan = orderdealview.SoDienThoai;
                dh.SoNha = orderdealview.SoNha;
                dh.Duong = orderdealview.Duong;
                dh.PhuongXa = orderdealview.PhuongXa;
                dh.QuanHuyen = orderdealview.QuanHuyen;
                dh.TinhThanh = orderdealview.TinhThanh;
                dataContext.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool InsertOrder(ShoppingCartOrderDealView shoppingorderview,CartModel Cart)
        {
            try
            {
                DonHang dh = new DonHang();
                int Price =0;
                List<DanhSachSanPham> list = new List<DanhSachSanPham>();
                dh.UsernameMember = shoppingorderview.OrderDealView.ThanhVien.Username;
                dh.TenNguoiNhan = shoppingorderview.OrderDealView.TenNguoiNhan;
                dh.SoDienThoaiNguoiNhan = shoppingorderview.OrderDealView.SoDienThoai;
                dh.TinhThanh = shoppingorderview.OrderDealView.TinhThanh;
                dh.QuanHuyen = shoppingorderview.OrderDealView.QuanHuyen;
                dh.PhuongXa = shoppingorderview.OrderDealView.PhuongXa;
                dh.Duong = shoppingorderview.OrderDealView.Duong;
                dh.SoNha = shoppingorderview.OrderDealView.SoNha;
                dh.HinhThucThanhToan = shoppingorderview.OrderDealView.HinhThucThanhToan;
                dh.TinhTrangDonHang = "Chưa Giao Hàng";
                for(int i =0 ;i< Cart.CartView.Count;i++)
                {
                    Price += Cart.CartView[i].PriceItem;
                }
                dh.TongTien = Price;
                dataContext.DonHangs.InsertOnSubmit(dh);
                dataContext.SubmitChanges();
                for(int i =0 ;i< Cart.CartView.Count;i++)
                {
                    DanhSachSanPham dssp = new DanhSachSanPham();
                     dssp.MaDonHang = dh.MaDonHang;
                    dssp.MaDeal = Cart.CartView[i].deal.MaDeal;
                    dssp.SoLuong =Cart.CartView[i].quantity;
                    dssp.TongTien = Cart.CartView[i].PriceItem;
                    list.Add(dssp);
                }
               
                dataContext.DanhSachSanPhams.InsertAllOnSubmit(list);
                dataContext.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public List<OrderDealView> GetListOrderPromotion()
        {
            List<DonHang> listdonhang = GetAllDonHang();
            List<OrderDealView> list = new List<OrderDealView>();
            for(int i=0;i<listdonhang.Count;i++)
            {
                OrderDealView temp = new OrderDealView();
          /*  if (maDeal < 0)
            {
                return list;
            }*/
            List<DanhSachSanPham> listorder = (from order in dataContext.DanhSachSanPhams
                                               where order.MaDonHang ==listdonhang[i].MaDonHang
                                               select order).ToList();
       
                ThanhVien thanhvien = dataContext.ThanhViens.Single(tv => tv.Username == listdonhang[i].UsernameMember);
      
                temp.DanhSachSanPham = listorder;
                temp.MaDonHang = listdonhang[i].MaDonHang;
                temp.NgayDat = listdonhang[i].NgayDat;
                temp.ThanhVien = thanhvien;
                temp.TinhTrang = listdonhang[i].TinhTrangDonHang;
                temp.TongCong =String.Format("{0:0,0}", listdonhang[i].TongTien);
                temp.SoDienThoai = listdonhang[i].SoDienThoaiNguoiNhan;
                temp.SoNha = listdonhang[i].SoNha;
                temp.Duong = listdonhang[i].Duong;
                temp.PhuongXa = listdonhang[i].PhuongXa;
                temp.QuanHuyen = listdonhang[i].QuanHuyen;
                temp.TinhThanh = listdonhang[i].TinhThanh;
                list.Add(temp);
            }
            return list;

        }
        public List<OrderDealView> GetListOrderPromotionByUsername(String Username)
        {
            List<DonHang> listdonhang  = (from order in dataContext.DonHangs
                                                        where order.UsernameMember == Username
                                                        select order).ToList();
            List<OrderDealView> list = new List<OrderDealView>();
            for (int i = 0; i < listdonhang.Count; i++)
            {
                OrderDealView temp = new OrderDealView();
                /*  if (maDeal < 0)
                  {
                      return list;
                  }*/
                List<DanhSachSanPham> listorder = (from order in dataContext.DanhSachSanPhams
                                                   where order.MaDonHang == listdonhang[i].MaDonHang
                                                   select order).ToList();

                ThanhVien thanhvien = dataContext.ThanhViens.Single(tv => tv.Username == listdonhang[i].UsernameMember);

                temp.DanhSachSanPham = listorder;
                temp.MaDonHang = listdonhang[i].MaDonHang;
                temp.NgayDat = listdonhang[i].NgayDat;
                temp.ThanhVien = thanhvien;
                temp.TinhTrang = listdonhang[i].TinhTrangDonHang;
                temp.HinhThucThanhToan = listdonhang[i].HinhThucThanhToan;
                temp.TongCong = String.Format("{0:0,0}", listdonhang[i].TongTien);
                temp.SoDienThoai = listdonhang[i].SoDienThoaiNguoiNhan;
                temp.SoNha = listdonhang[i].SoNha;
                temp.Duong = listdonhang[i].Duong;
                temp.PhuongXa = listdonhang[i].PhuongXa;
                temp.QuanHuyen = listdonhang[i].QuanHuyen;
                temp.TinhThanh = listdonhang[i].TinhThanh;
                list.Add(temp);
            }
            return list;

        }
        public List<OrderDealView> GetListOrderPromotion(int madeal)
        {
            List<OrderDealView> list = new List<OrderDealView>();
            if (madeal < 0)
            {
                return list;
            }
            List<DanhSachSanPham> listorder = (from order in dataContext.DanhSachSanPhams
                                               where order.MaDeal == madeal
                                               select order).ToList();
            foreach(DanhSachSanPham sp in listorder)
            {
                OrderDealView ord = new OrderDealView();
                ord.MaDonHang = sp.MaDonHang;
                ord.MaDeal = sp.MaDeal;
                ord.NgayDat = sp.DonHang.NgayDat;
                ord.SoLuong = sp.SoLuong;
                ord.TinhTrang = sp.DonHang.TinhTrangDonHang;
                ord.TongCong = String.Format("{0:0,0}", sp.TongTien);
                list.Add(ord);
            }
            return list;

        }
        public OrderDealView GetDonHang(int id)
        {
            OrderDealView temp = new OrderDealView();
            DonHang donhang =dataContext.DonHangs.Single(tv => tv.MaDonHang == id);
            List<DanhSachSanPham> listorder = (from order in dataContext.DanhSachSanPhams
                                               where order.MaDonHang == donhang.MaDonHang
                                               select order).ToList();
            ThanhVien thanhvien = dataContext.ThanhViens.Single(tv => tv.Username == donhang.UsernameMember);

            temp.DanhSachSanPham = listorder;
            temp.DonHang = donhang;
            temp.MaDonHang =donhang.MaDonHang;
            temp.TenNguoiNhan = donhang.TenNguoiNhan;
            temp.NgayDat = donhang.NgayDat;
            temp.ThanhVien = thanhvien;
            temp.TinhTrang = donhang.TinhTrangDonHang;
            temp.TongCong = String.Format("{0:0,0}", donhang.TongTien);
            temp.SoDienThoai = donhang.SoDienThoaiNguoiNhan;
            temp.SoNha = donhang.SoNha;
            temp.Duong = donhang.Duong;
            temp.PhuongXa = donhang.PhuongXa;
            temp.QuanHuyen = donhang.QuanHuyen;
            temp.TinhThanh = donhang.TinhThanh;

            return temp;
        }
    }
}